@extends('admin.layout')
@section('title')
    订单列表
@endsection
@section('body')

    <div class="widget-body">
        <form role="form" class="form-inline widget">
            <div class="form-group">
                <input type="text" name="username" placeholder="用户名" class="form-control" value="{{$condition['username']}}">
            </div>
            <div class="form-group">
              <input type="text" name="order_sn"  placeholder="订单号" class="form-control" value="{{$condition['order_sn']}}">
            </div>

            <div class="form-group">
                     订单状态:
                    <select name="order_status">
                        <option value="-1">-请选择-</option>
                        <option @if($condition['order_status'] == 0) selected  @endif value="0">-未确认-</option>
                        <option @if($condition['order_status'] == 1) selected  @endif value="1">-已确认-</option>
                        <option @if($condition['order_status'] == 2) selected  @endif value="2">-已取消-</option>
                        <option @if($condition['order_status'] == 3) selected  @endif value="3">-无效-</option>
                        <option @if($condition['order_status'] == 4) selected  @endif value="4">-退货-</option>
                    </select>
            </div>
            <div class="form-group">
                    发货状态:
                    <select name="shipping_status">
                        <option value="-1">-请选择-</option>
                        <option @if($condition['shipping_status'] == 0) selected  @endif value="0">-未发货-</option>
                        <option @if($condition['shipping_status'] == 1) selected  @endif value="1">-已发货-</option>
                        <option @if($condition['shipping_status'] == 2) selected  @endif value="2">-已收货-</option>
                        <option @if($condition['shipping_status'] == 4) selected  @endif value="4">-退货-</option>
                    </select>
            </div>
            <div class="form-group">
                    支付状态:
                    <select name="pay_status">
                        <option value="-1">-请选择-</option>
                        <option @if($condition['pay_status'] == 0) selected  @endif value="0">-未付款-</option>
                        <option @if($condition['pay_status'] == 1) selected  @endif value="1">-付款中-</option>
                        <option @if($condition['pay_status'] == 2) selected  @endif value="2">-已付款-</option>
                    </select>
            </div>
            <button class="btn btn-default" type="submit">搜索</button>
        </form>

        <div class="widget">
            <table class="table table-bordered table-hover">
                <thead>
                <tr>
                    <th>订单ID</th>
                    <th>订单号</th>
                    <th>用户名</th>
                    <th>发货状态</th>
                    <th>支付状态</th>
                    <th>收货人</th>
                    <th>收货地址</th>
                    <th>订单状态</th>
                    <th>快递单号</th>
                    <th>收货人手机</th>
                    <th>配送方式</th>
                    <th>应付金额</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                @foreach($orders as $key=>$order)
                    <tr>
                        <td>{{$order['id']}}</td>
                        <td>{{$order['order_sn']}}</td>
                        <td>{{$order['username']}}</td>
                        <td>
                            <?php
                            switch ($order['shipping_status']) {
                                case 0:
                                    echo '未发货';
                                    break;
                                case 1:
                                    echo '已发货';
                                    break;
                                case 2:
                                    echo '已收货';
                                    break;
                                case 4:
                                    echo '退货';
                                    break;
                            }
                            ?>
                        </td>
                        <td>
                            <?php
                            switch ($order['pay_status']) {
                                case 0:
                                    echo '未付款';
                                    break;
                                case 1:
                                    echo '付款中';
                                    break;
                                case 2:
                                    echo '已付款';
                                    break;
                            }
                            ?>
                        </td>
                        <td>{{$order['consignee']}}</td>
                        <td>{{$order['province']}}{{$order['city']}}{{$order['district']}}{{$order['address']}}</td>
                        <td>
                            <select name="order_status" order_sn="{{$order['order_sn']}}" class="c_order_status">
                                <option value="0" @if($order['order_status']==0) selected @endif>未确认</option>
                                <option value="1" @if($order['order_status']==1) selected @endif>确认</option>
                                <option value="2" @if($order['order_status']==2) selected  @endif>已取消</option>
                                <option value="3" @if($order['order_status']==3) selected  @endif>无效</option>
                                <option value="4" @if($order['order_status']==4) selected  @endif>退货</option>
                            </select>
                        </td>
                        <td><input type="text" value="{{$order['invoice_no']}}" placeholder="快递单号"  order_sn="{{$order['order_sn']}}" class="c_shipping_status"/> </td>
                        <td>{{$order['mobile']}}</td>
                        <td>{{$order['shipping_id']}}</td>
                        <td>{{$order['order_amount']}}</td>
                        <td>
                            <a href="{{action('Admin\OrderController@getOrderDetail',['id'=>$order['id']])}}">详情</a>
                            @if($order['shipping_status'] == 0)
                                |<a href="#" id="{{$order['id']}}" onclick="sendGoods({{$order['id']}});">发货</a>
                            @endif
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
        <div class="widget">
            <div class="pull-left">共{{ $orders->total() }}条记录</div>
            <div class="pull-right">{!! $orders->appends(request()->all())->render() !!}</div>
            <div style="clear: both"></div>
        </div>
    </div>
    </div>
@endsection
@section('foot_script')
    <script type="text/javascript">
        function sendGoods(id){
            upOrder('shipping_status',1,'id',id);
        }

        $(function(){
            //修改订单状态
            $('.c_order_status').change(function(){
                $order_sn = $(this).attr('order_sn');
                $status = $(this).val();
                upOrder('order_status',$status,'order_sn',$order_sn);

            });
            //录入发货单号
            $('.c_shipping_status').blur(function(){
                $order_sn = $(this).attr('order_sn');
                $shipping_val = $(this).val();

                upOrder('invoice_no',$shipping_val,'order_sn',$order_sn);

            });
        });

        function upOrder(field,fieldval,wherefield,whereval){
            if($.trim(fieldval)==''){
                return false;
            }
            $.ajax({
                url:'{{action(NAMESPACE_ADMIN.'OrderController@getUp')}}',
                dataType:'json',
                data:{'field':field,'fieldval':fieldval,'wherefield':wherefield,'whereval':whereval,'_token':"{{csrf_token()}}"},
                success:function(data){
                    if(data.code == 200){
                        location.reload();
                    }
                },
                error:function(){

                }
            });
        }

    </script>
@endsection